package com.taojie.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taojie.graduation.domain.Car;
import com.taojie.graduation.domain.User;
import com.taojie.graduation.mapper.UserMapper;
import com.taojie.graduation.service.api.CarService;
import com.taojie.graduation.service.api.UserService;
import com.taojie.graduation.utils.PageResult;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private CarService carService;

    /**
     * TODO 分页查询用户
     * @param page 页码
     * @param size 每页数量
     * @param query 查询条件 昵称或者真实姓名都可以
     * @return {@link Result}
     */
    @Override
    public Result findUserPage(int page, int size, String query) {
        Page<User> userPage = new Page<>(page, size);
        QueryWrapper<User> wapper = null;
        if (!StringUtils.isEmpty(query)) {
            wapper = new QueryWrapper<>();
            wapper.like("nick_name", query).or().like("real_name", query);
        }
        Page<User> userPage1 = userMapper.selectPage(userPage, wapper);
        // 根据用户id排序
        List<User> collect = userPage1.getRecords().stream().sorted(Comparator.comparing(User::getId)).collect(Collectors.toList());
        if (!(userPage1.getRecords().size() > 0)) {
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS, new PageResult<User>(userPage1.getTotal(), collect));
    }

    /**
     * TODO 根据id查询用户
     * @return {@link Result}
     */
    @Override
    public Result findUserById(Integer id) {
        User user = userMapper.selectById(id);
        if (ObjectUtils.isEmpty(user)) {
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS, user);
    }

    /**
     * TODO 修改用户
     * @param user
     * @return {@link Result}
     */
    @Override
    public Result updateUserById(User user) {
        int i = userMapper.updateById(user);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteUserById(Integer id) {
        if (id < 0) { return Result.INPUT_ERROR(); }
        int i = userMapper.deleteById(id);
        carService.deleteByUserId(id);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 添加用户
     * @param user
     * @return
     */
    @Override
    public Result insertUser(User user) {
        if (ObjectUtils.isEmpty(user)){
            return Result.INPUT_ERROR();
        }
        int i = userMapper.insert(user);
        // 根据用户昵称或者真实姓名查找用户，并完成购物车的添加
        User user1 = null;
        if (!StringUtils.isEmpty(user.getRealName())){
            user1 = findByRealName(user.getRealName());
        } else {
            user1 = findByNickName(user.getNickName());
        }
        carService.add(new Car(), user1);

        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据真实姓名查找用户
     * @param realName
     * @return
     */
    @Override
    public User findByRealName(String realName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("real_name", realName);
        return userMapper.selectOne(wrapper);
    }

    /**
     * TODO 根据昵称查找用户
     * @param nickName
     * @return
     */
    @Override
    public User findByNickName(String nickName){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name", nickName);
        return userMapper.selectOne(wrapper);
    }
}
